<?php


namespace app\api\service;



use think\Config;

class CashServiceYlys
{
    //配置信息
    private $config;
    //是否绑卡需要异步通知
    public $bcardneedotifiy;
    public function __construct()
    {
        if (Config::has('cashconfigylys')){
            $this->config = Config::get('cashconfigylys');
        }else{
            $config = Config::load(ROOT_PATH.'application/api/config.php');
            $this->config = $config['cashconfigylys'];
            Config::set('cashconfigylys',$this->config);
        }
        $this->bcardneedotifiy = 0;
    }
    public function test(){
        print_r($this->config);
    }
    /**
     * 生成签名
     * @param array $data
     */
    private function signs(array $data){
        //将数组转为json 字符串
        $data_json = json_encode($data);
        //将json格式字符串转换为字节数组（编码格式:UTF-8）; ok
        //$data_bytes = Bytes::getBytes($data_json);
        $public_key = "-----BEGIN PUBLIC KEY-----\n".$this->config['pub_key']."\n-----END PUBLIC KEY-----";
        //使用公钥将数据加密
        $encrypt_data = RsaUtils::publicEncrypt($data_json,$public_key);
        return $encrypt_data;
    }

    /**
     * 发送数据
     * @param string $url
     * @param array $params
     * @return mixed
     */
    private function send(string $url,array $params){
        $map['data'] = $this->signs($params);
        $map['appId'] = $this->config['appid'];
        $res_string =  curlPost($url,$map);
        return json_decode($res_string,true);
    }

    /**
     * 商户注册
     * @param array $data
     * @return mixed
     */
    public function register(array $data){
        $url = 'http://unionpay.rytpay.com.cn/rytpay-business/creditcard/api/rh.do?regist';
        //组织数据
        $param = [];
        //开户姓名	M
        $param['realName'] = $data['name'];
        //开户证件号	M
        $param['certNo'] = $data['idno'];
        //银行卡号	M
        $param['toBankNo'] = $data['acctid'];
        //银行卡绑定手机号	M
        $param['toPhoneNo'] = $data['mobile'];
        //统一整理返回数据***
//        $res =  $this->send($url,$param);
//        $res_data['code'] = $res['respCode'] == '0000'?1:$res['respCode'];
//        $res_data['msg'] = $res['respMsg'];
//        $res_data['merchantno'] = isset($res['merchantNo'])?$res['merchantNo']:'';
        $res_data['code'] = 1;
        $res_data['msg'] = '注册成功';
        $res_data['merchantno'] = rand(1000000,9999999);
        return $res_data;
    }

    /**
     * 商户业务开通
     * @param array $data
     * @return mixed
     */
    public function opening(array $data){
        $url = 'http://unionpay.rytpay.com.cn/rytpay-business/creditcard/api/rh.do?openproduct';
        //组织数据
        $param = [];
        //merchantNo   rate
        $param['merchantNo'] = $data['merchantno'] ;
        $param['rate'] = $data['rate'];
//        $res =  $this->send($url,$param);
//        $res_data['code'] = $res['respCode'] == '0000'?1:$res['respCode'];
//        $res_data['msg'] = $res['respMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }

    /**
     * 修改结算银行卡
     * @param array $data
     * @return mixed
     */
    public function changeBcard(array $data){
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }
    /**
     * 修改费率
     */
    public function changeRate($data){
        $url = 'http://unionpay.rytpay.com.cn/rytpay-business/creditcard/api/rh.do?change/rate';
        //组织数据
        $param = [];
        $param['merchantNo'] = $data['merchantno'];
        $param['rate'] = $data['rate']*0.01;
//      $res =  $this->send($url,$param);
//      $res_data['code'] = $res['respCode'] == '0000'?1:$res['respCode'];
//      $res_data['msg'] = $res['respMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }

    /**
     * 获取商户余额
     */
    public function getbalance(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

    /**
     * 收款商户mcc查询
     */
    public function getmcc(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

    /**
     * 获取收款商户信息
     */
    public function getbussinessinfo(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

    /**
     * consumption 绑银行卡卡接口
     */
    public function bindcards(){
        return $res['code'] = 1;
    }

    /**
     * 绑卡同步/异步接口
     */
    public function  bind_notifiy(){
        return true;
    }
    /**
     *consumption 结算银行卡解绑接口
     */
    public function unbindcards(){
        return true;
    }

    /**
     * 绑定消费信用卡
     * @param array $data
     * @return mixed
     */
    public function bindCcard(array $data){
        $url = 'http://unionpay.rytpay.com.cn/rytpay-business/creditcard/api/rh.do?binding';
        //组织数据
        $param = [];
        //订单编号
        $param['orderNo'] = $data['orderNo'];
        //通道商户编号
        $param['merchantNo'] = $data['merchantno'];
        //户名
        $param['realName'] = $data['name'];
        //账号
        $param['accNo'] = $data['acctid'];
        //卡预留手机号
        $param['phoneNo'] = $data['mobile'];
        //证件号
        $param['certNo'] = $data['idno'];
        //结算户类型
        $param['backUrl'] = 'http://www.cash.com/notifiyjft.php?act=bindccard';
//        $res =  $this->send($url,$param);
//        $res_data['code'] = $res['respCode'] == '0000'?1:$res['respCode'];
//        $res_data['msg'] = $res['respMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }

    /**
     * 解绑消费信用卡
     * @param array $data
     */
    public function unbindCcard(array $data){
        $url = '';
        //组织数据
        $param = [];
        $param['merchantNo'] = $data['merchantno'];
        $param['accNo'] = $data['acctid'];
//        $res =  $this->send($url,$param);
//        $res_data['code'] = $res['respCode'] == '0000'?1:$res['respCode'];
//        $res_data['msg'] = $res['respMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }
    /**
     * 银行卡绑卡查询接口
     */
    public function find_cards(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

    /**
     *消费接口
     * @param array $data
     * @return mixed
     */
    public function pay(array $data){
        $url = '';
        //组织数据
        $param = [];
        $param['orderNo'] = $data['order_no'];
        $param['merchantNo'] = $data['merchantno'];
        $param['accNo'] = $data['accno'];
        $param['phoneNo'] = $data['phoneno'];
        $param['realName'] = $data['realname'];
        $param['certNo'] = $data['certno'];
        $param['txnAmt'] = $data['totle_money'];
        $param['rate'] = $data['rate'];
        $param['province'] = $data['province'];
        $param['city'] = $data['city'];
        $param['orderIp'] = $data['user_ip'];
        $param['superMerchantNo'] = '';
        $param['backUrl'] = 'http://www.cash.com/notifiyjft.php?act=pay_notifiy';
        //$res = $this->send($url,$param);
//        $res_data['code'] = $res['resCode'] == '0000'?1:$res['resCode'];
//        $res_data['msg'] = $res['resMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }

    /**
     * 消费异步通知接口
     */
    public function pay_notifiy(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

    /**
     * 提现接口
     */
    public function cash($data){
        $url = '';
        //组织数据
        $param = [];
        $param['orderNo'] = $data['order_no'];
        $param['merchantNo'] = $data['merchantno'];
        $param['txnAmt'] = $data['order_price'];
        $param['advanceFee'] = $data['advancefee'];
        $param['bankNo'] = $data['accno'];
        $param['bankName'] = $data['bankname'];
        $param['realName'] = $data['realname'];
        $param['certNo'] = $data['certno'];
        //$res = $this->send($url,$param);
//        $res_data['code'] = $res['resCode'] == '0000'?1:$res['resCode'];
//        $res_data['msg'] = $res['resMsg'];
        $res_data['code'] = 1;
        $res_data['msg'] = 'ok';
        return $res_data;
    }

    /**
     * 订单查询接口
     */
    public function find_orders(){
        $url = '';
        //组织数据
        $param = [];
        return $this->send($url,$param);
    }

}